package com.rimi.servlet;

import com.rimi.bean.JsonResult;
import com.rimi.bean.Menu;
import com.rimi.bean.Role;
import com.rimi.bean.User;
import com.rimi.service.MenuService;
import com.rimi.service.RoleService;
import com.rimi.service.UserService;
import com.rimi.service.impl.MenuServiceImpl;
import com.rimi.service.impl.RoleServiceImpl;
import com.rimi.service.impl.UserServiceImpl;
import com.rimi.util.StringUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author chengliang
 * 创建时间 2020/3/26 11:30
 * 用来处理跳转登录页面的servlet
 */
@WebServlet("/index.html")
public class IndexServlet  extends HttpServlet {

    private UserService userService = new UserServiceImpl();
    private RoleService roleService = new RoleServiceImpl();
    private MenuService menuService = new MenuServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行我的servlet");
        String userName = null;
        String password = null;
        //登录逻辑
        Cookie[] cookies = req.getCookies();
        if(cookies != null){
            for (Cookie cookie : cookies) {
                if("username".equals(cookie.getName())){
                    userName = cookie.getValue();
                }
                if("password".equals(cookie.getName())){
                    password = cookie.getValue();
                }
            }
            //账号 || 密码 cookie中没有账号和密码信息
            if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)){
                //跳转登录页面进行登录
                resp.sendRedirect("/index.jsp");
                return;
            }
        }
        //使用账号与密码进行登录
        //验证登录信息
        User user = userService.login(userName, password);
        if(user == null){
            //跳转登录页面进行登录
            resp.sendRedirect("/index.jsp");
            return;
        }
        //验证账号是否被禁用
        if(user.getUserEnable() == 1){
            //跳转登录页面进行登录
            resp.sendRedirect("/index.jsp");
            return;
        }
        //该账户的角色被禁用
        Role role = roleService.getRoleByRoleId(user.getRoleId());
        if(role == null || role.getRoleEnable() == 1){
            //跳转登录页面进行登录
            resp.sendRedirect("/index.jsp");
            return;
        }
        //
        ServletContext servletContext = req.getServletContext();
        //登录成功,将用户添加到用户列表中
        //重写比较器
        Set<User> onlineUser = new HashSet<>();
        //判断在线用户列表中是否存在用户
        Object object = servletContext.getAttribute("onlineUser");
        if(object != null){
            onlineUser = (Set<User>)object;
        }
        boolean isok = true;
        //检查在线用户列表中 是否存在该用户
        for (User u : onlineUser) {
            if(u.getUserId().equals(user.getUserId())){
                isok = false;
            }
        }
        if(isok){
            onlineUser.add(user);
        }
        //存储到ServletContext中
        servletContext.setAttribute("onlineUser",onlineUser);
        //将账户拥有的菜单项 显示到页面中
        List<Menu> menuList = menuService.getMenuListByRoleId(user.getRoleId());
        HttpSession session = req.getSession();
        session.setAttribute("user",user);
        session.setAttribute("menuList",menuList);
        //登录成功回到首页
        req.getRequestDispatcher("/page/index.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
